package chaoyue.study.thread;

/**
 * 设置线程优先级
 * 优先级的范围是 1-10 如果不设置优先级 默认的优先级是5
 * 优先级高的线程抢到CPU时间片的概率会高一些（理论上）
 * 但实际开发中不要依赖这个优先级，有很大概率不起什么作用
 */
public class ThreadPriority {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            for (int i = 0; i < 1000; i++) {
                System.out.println(threadName + " --> " + i);
            }
        }, "t1");
        t1.setPriority(Thread.MIN_PRIORITY); // 设置优先级为最小优先级

        Thread t2 = new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            for (int i = 0; i < 1000; i++) {
                System.out.println(threadName + " --> " + i);
            }
        }, "t2");
        t2.setPriority(Thread.MAX_PRIORITY);// 这只优先级为最大优先级 让系统尽量优先执行这个线程

        t1.start();
        t2.start();
    }
}
